package com.chenfan.common.extension.mybatisplus.assembler;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chenfan.common.extension.context.UserContextHolder;
import com.chenfan.common.extension.model.*;
import com.chenfan.common.vo.UserVO;
import org.apache.commons.lang3.ObjectUtils;

import java.util.Date;

/**
 * DB交互参数对象装配者实现
 *
 * @author wulg
 * @date 2021-09-13
 **/
public class DefaultParameterObjectAssembler implements ParameterObjectAssembler {

    /**
     * 校验是否是Table实体对象进行DML操作
     * 注：规范上禁止非Table实体对象(包括继承的)进行DML操作
     *
     * TODO 老代码未做规范化改造，暂不做保护  return entityClass.isAnnotationPresent(TableName.class);
     * @return
     */
    @Override
    public boolean supportDml(Class<?> entityClass) {
        return true;
    }

    /**
     * 插入填充
     * @param parameter
     */
    @Override
    public void insertFill(Object parameter) {
        if(parameter instanceof BaseCreateEntity) {
            this.fillCreateInfo((BaseCreateEntity)parameter);
        }
        if(parameter instanceof BaseSaveEntity) {
            this.updateFill(parameter);
        }
        if(parameter instanceof BaseTenantEntity) {
            this.fillTenantInfo((BaseTenantEntity)parameter);
        }
        if(parameter instanceof BaseEntity) {
            this.fillCompanyAndDepartmentInfo((BaseEntity)parameter);
        }
        if(parameter instanceof BaseOwnerEntity) {
            this.fillOwnerInfo((BaseOwnerEntity)parameter);
        }
    }

    /**
     * 更新填充
     * @param parameter
     */
    @Override
    public void updateFill(Object parameter) {
        if(parameter instanceof BaseSaveEntity) {
            BaseSaveEntity updateEntity = (BaseSaveEntity)parameter;
            UserVO userVO = UserContextHolder.getUserVo();
            if (userVO != null) {
                updateEntity.setUpdateBy(userVO.getUserId());
                updateEntity.setUpdateName(userVO.getRealName());
                updateEntity.setUpdateDate(new Date());
            } else if(updateEntity.getUpdateDate() == null) {
                updateEntity.setUpdateDate(new Date());
            }
        } else if(parameter instanceof LambdaUpdateWrapper) {
            LambdaUpdateWrapper lambdaUpdateWrapper = (LambdaUpdateWrapper)parameter;
            // 不好处理...
        }
    }

    /**
     * 重要字段-防更新
     *          * 注意：如果要更新保护字段，请不要继承Base相关对象
     *          * 保护createBy/createName/createDate 不被更新 目前很多都是查询出的实体对象直接更新，暂不做保护
     *          * if(parameter instanceof BaseCreateEntity) {
     *          *             BaseCreateEntity baseCreateEntity = (BaseCreateEntity)parameter;
     *          *             baseCreateEntity.setCreateBy(null);
     *          *             baseCreateEntity.setCreateName(null);
     *          *             baseCreateEntity.setCreateDate(null);
     *          *         }
     *          *         保护tenantId、companyId、departmentId不被更新
     * @param parameter
     */
    @Override
    public void preventUpdateOfImportantField(Object parameter) {
        //保护tenantId不被更新
        if(parameter instanceof BaseTenantEntity) {
            BaseTenantEntity baseTenantEntity = (BaseTenantEntity)parameter;
            baseTenantEntity.setTenantId(null);
        }
        //保护companyId/departmentId 不被更新
        if(parameter instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity)parameter;
            baseEntity.setCompanyId(null);
            //baseEntity.setDepartmentId(null);
        }
        //保护ownerId/ownerName 不被更新
        if(parameter instanceof BaseOwnerEntity) {
            BaseOwnerEntity baseOwnerEntity = (BaseOwnerEntity)parameter;
            baseOwnerEntity.setOwnerId(null);
            baseOwnerEntity.setOwnerName(null);
        }
    }


    /**
     * 填充创建人信息
     * @param createEntity
     */
    private void fillCreateInfo(BaseCreateEntity createEntity) {
        /*if(Objects.nonNull(createEntity.getId())) {
            return;
        }*/
        UserVO userVO = UserContextHolder.getUserVo();
        if (userVO != null) {
            createEntity.setCreateBy(userVO.getUserId());
            createEntity.setCreateName(userVO.getRealName());
            createEntity.setCreateDate(new Date());
        } else if(createEntity.getCreateDate() == null) {
            createEntity.setCreateDate(new Date());
        }
        createEntity.setIsDelete(ObjectUtils.defaultIfNull(createEntity.getIsDelete(),0));
    }

    /**
     * 填充租户信息
     * @param baseTenantEntity
     */
    private void fillTenantInfo(BaseTenantEntity baseTenantEntity) {
        UserVO userVO = UserContextHolder.getUserVo();
        if (userVO != null) {
            baseTenantEntity.setTenantId(userVO.getTenantId());
        }
    }

    /**
     * 填充公司/部门信息
     * @param baseEntity
     */
    private void fillCompanyAndDepartmentInfo(BaseEntity baseEntity) {
        UserVO userVO = UserContextHolder.getUserVo();
        if (userVO != null) {
            baseEntity.setCompanyId(userVO.getCompanyId());
        }
    }

    /**
     * 填充数据所有人
     * @param baseOwnerEntity
     */
    private void fillOwnerInfo(BaseOwnerEntity baseOwnerEntity) {
        UserVO userVO = UserContextHolder.getUserVo();
        if (userVO != null) {
            baseOwnerEntity.setOwnerId(userVO.getUserId());
            baseOwnerEntity.setOwnerName(userVO.getRealName());
        }
    }
}
